package com.css.fxfzypg.modules.resultsManagement.repository.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.resultsManagement.entity.MapTemplate;
import com.css.fxfzypg.modules.resultsManagement.repository.MapTemplateRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MapTemplateRepositoryImpl implements MapTemplateRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveMapTemplate(MapTemplate mapTemplate) {
        String sql = "insert into map_template_management (id,map_type,map_type_id,dict_type,dict_code,report_type,enable_flag,template_id," +
                "template_name,format,uploader,upload_time,del_flag,create_user,create_time,sm_template_name) values " +
                "(:id,:mapType,:mapTypeId,:dictType,:dictCode,:reportType,:enableFlag,:templateId,:templateName,:format," +
                ":uploader,:uploadTime,:delFlag,:createUser,:createTime,:smTemplateName)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(mapTemplate);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public MapTemplate getById(String id) {
        String sql = "select * from map_template_management where id ='" + id + "' and del_flag ='" + YNEnum.N.toString() + "'";
        MapTemplate mapTemplate = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapTemplate.class));
        return mapTemplate;
    }

    @Override
    public void deleteById(String id, Timestamp currentTimestamp) {
        String sql = "update map_template_management set del_flag = '" + YNEnum.Y + "' , update_time = '" + currentTimestamp + "' " + " where id ='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public MapTemplate update(MapTemplate mapTemplate) {
        String sql = "update map_template_management set map_type_id=:mapTypeId,dict_type=:dictType,dict_code=:dictCode,map_type=:mapType,report_type=:reportType," +
                "enable_flag=:enableFlag,template_id=:templateId,template_name=:templateName,format=:format," +
                "uploader=:uploader,upload_time=:uploadTime,del_flag=:delFlag,update_user=:updateUser," +
                "update_time=:updateTime where id=:id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(mapTemplate);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return mapTemplate;
    }

    @Override
    public Map<String, Object> queryMapTemplates(MapTemplate mapTemplate, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from map_template_management ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '" + YNEnum.N.toString() + "' ");
        if (!PlatformObjectUtils.isEmpty(mapTemplate.getReportType())) {
            whereSql.append(" and report_type = '" + mapTemplate.getReportType() + "' ");
        }
        if (!PlatformObjectUtils.isEmpty(mapTemplate.getUploader())) {
            whereSql.append(" and uploader like '%" + mapTemplate.getUploader() + "%' ");
        }
        if (!PlatformObjectUtils.isEmpty(mapTemplate.getStartTime()) && !PlatformObjectUtils.isEmpty(mapTemplate.getEndTime())) {
            whereSql.append(" and upload_time between '" + mapTemplate.getStartTime() + "' and '" + mapTemplate.getEndTime() + "'");
        }
        StringBuilder orderSql = new StringBuilder(" order by upload_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder sql1 = sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql1.toString(), new BeanPropertyRowMapper<>(MapTemplate.class)).size();
        String sql2 = sql1.append(orderSql).append(limitSql).toString();
        List<MapTemplate> maps = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<>(MapTemplate.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", maps);
        return map;
    }

    @Override
    public List<MapTemplate> query(String reportType) {
        StringBuilder sql = new StringBuilder("select * from map_template_management ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '" + YNEnum.N.toString() + "' and enable_flag ='" + YNEnum.Y.toString() + "'");
        whereSql.append(" and report_type = '" + reportType + "' ");
        List<MapTemplate> maps = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(MapTemplate.class));
        return maps;
    }

    @Override
    public List<MapTemplate> getByTypeId(String id) {
        String sql = "select * from map_template_management where map_type_id ='" + id + "' and del_flag ='" + YNEnum.N.toString() + "'";
        List<MapTemplate> mapTemplates = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MapTemplate.class));
        return mapTemplates;
    }

    @Override
    public void delete(String id, Timestamp timestamp) {
        String sql = "update map_template_management set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' " + " where map_type_id ='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateFlag(String id, String enableFlag, Timestamp timestamp) {
        String sql = "update map_template_management set enable_flag = '" + enableFlag + "' , update_time = '" + timestamp + "' " + " where map_type_id ='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public MapTemplate getByTypeIdAndFormat(String mapTypeId, String format) {
        String sql = "select * from map_template_management where map_type_id ='" + mapTypeId + "' and del_flag ='" + YNEnum.N.toString() + "' and format = '" + format + "'";
        try {
            MapTemplate mapTemplate = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapTemplate.class));
            return mapTemplate;
        } catch (DataAccessException e) {
            return null;
        }
    }
}
